﻿CREATE PROCEDURE [dbo].[s_ExportFromMegasys_AddOn]
	@LoanID int
AS
SET NOCOUNT ON;

BEGIN TRAN s_ExportFromMegasys_AddOn

IF NOT EXISTS(SELECT * FROM Borrower(@LoanID,DEFAULT) )BEGIN
	EXEC sh_Debug_Print 'EXEC s_Borrower_Add $0',@LoanID
	EXEC s_Borrower_Add @LoanID
END

EXEC sh_Debug_Print 'UPDATE v_Borrower_0'
UPDATE v_Borrower_0  -- Borrower 
SET SSN = RTRIM(XSOC_SEC),
		FirstName = RTRIM(LTRIM(dbo.GetTokenRight(XFA_NAME, N','))),
		LastName = dbo.GetTokenLeft(XFA_NAME, N','),
		PhoneHome = RTRIM(XFA_HOMEP),
		DOBYear = YEAR(XFA_BDATE),DOBMonth = MONTH(XFA_BDATE),DOBDay = DAY(XFA_BDATE)
FROM v_Borrower_0 b
INNER JOIN t_Loan l ON l.ID = b.LoanID
INNER JOIN v_ExportFromMegasys e ON e.XFA_CLASS = 10 AND e.XFA_ACCOUNT = RIGHT(l.MsID,6)
WHERE l.ID = @LoanID

EXEC sh_Debug_Print 'UPDATE v_Address'
UPDATE v_Address -- Address
SET Street = LTRIM(RTRIM(XFA_HOUSENO))+' '+LTRIM(RTRIM(XFA_STNAME)),
		CityName = RTRIM(XFA_CITY),StateCode = RTRIM(XSTATE_CODE),ZIP = RTRIM(XZIP_CD)
FROM v_Address a
INNER JOIN t_Loan l ON l.ID = a.LoanID AND a.BorrowerPos = 0 AND a.AddressPos = 0 AND a.AddressTypeID = 1
INNER JOIN v_ExportFromMegasys e ON e.XFA_CLASS = 10 AND e.XFA_ACCOUNT = RIGHT(l.MsID,6)
WHERE l.ID = @LoanID

EXEC sh_Debug_Print 'COMMIT TRANSACTION s_ExportFromMegasys_AddOn'
COMMIT TRANSACTION s_ExportFromMegasys_AddOn





